package com.jinlejia.erp.ui.mine.transfer;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityTransferCheckBinding;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.jinlejia.erp.ui.mine.adapter.TransferCheckAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * 异动选择页面
 * 异动类型--type=0，异动后职务--type =2，行政部门--type=3
 */
public class TransferCheckActivity extends AppCompatActivity {

    private String type;
    private TransferCheckAdapter adapter;
    private ArrayList<Map<String, Object>> dataList = new ArrayList<>();
    private ArrayList<Map<String, Object>> recruitmentChannelsList = new ArrayList<>();
    private Boolean lower;
    private String userLevel;

    private ProgressDialog progressDialog;
    private ActivityTransferCheckBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_transfer_check);
        initView();
        initData();
        initListener();
    }

    private void initData() {
        progressDialog = new ProgressDialog(this, "加载中...");
        adapter = new TransferCheckAdapter(dataList, TransferCheckActivity.this);
        adapter.changeMode(YfListMode.MODE_DATA);
        binding.contractRecyclerView.setHasFixedSize(true);
        binding.contractRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        binding.contractRecyclerView.setAdapter(adapter);
//        adapter.setData(dataList);
        type = getIntent().getStringExtra("type");
        int checkId = getIntent().getIntExtra("checkId", -1);
        int checkId2 = getIntent().getIntExtra("checkId2",-1);
//        Toast.makeText(this,""+checkId,Toast.LENGTH_SHORT).show();
        if (checkId != -1&&type.equals("2")) {
            adapter.setCheckId(checkId);
        }else if (checkId2 != -1&&type.equals("4")){
            adapter.setCheckId(checkId2);
        }
        getData();
    }

    private void initView() {
        binding.include.backBtn.setOnClickListener(view -> finish());
        if ("0".equals(type)) {
            binding.include.tvTitle.setText("选择异动类型");
        }else if ("2".equals(type)) {
            binding.include.tvTitle.setText("选择异动后职务");
        } else if ("3".equals(type)) {
            binding.include.tvTitle.setText("选择行政部门");
        }else if ("4".equals(type)){
            binding.include.tvTitle.setText("选择带训标识");
        }
    }

    private void getData() {
        progressDialog.show();
        OkHttpUtils
                .get()
                .url(ApiConfig.TRANSFER_LIST)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        if (DataFilter.filter(TransferCheckActivity.this, response, null)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            if ("0".equals(type)) {//异动类型
                                dataList = JSON.parseObject(data.getJSONArray("userbdTypeList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                recruitmentChannelsList = JSON.parseObject(data.getJSONArray("recruitChannelList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                lower = (Boolean) data.get("lower");
                            }else if ("2".equals(type) && null != data && !"".equals(data.toString())) {//异动后职务
                                dataList = JSON.parseObject(data.getJSONArray("dutyList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                            } else if ("3".equals(type)) {//行政部门
                                dataList = JSON.parseObject(data.getJSONArray("deptList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                            }else if ("4".equals(type)){//带训标识
                                dataList = JSON.parseObject(data.getJSONArray("trainingFlagList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                            }
                            userLevel = data.get("userLevel").toString();
                            adapter.setData(dataList);
                        }
                    }
                });

    }


    private void initListener() {
    }


    public void setResultData(int checkedId) {
        Intent intent = new Intent();
        if ("0".equals(type)) {//异动类型|
            intent.putExtra("lower", lower);
            //异动类型关联list传递
            Bundle bundle = new Bundle();
            bundle.putSerializable("reasonsForChangeList",(Serializable)JSON.parseObject(dataList.get(checkedId).get("list").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
            }));
            bundle.putSerializable("recruitChannelList",(Serializable)recruitmentChannelsList);
            intent.putExtras(bundle);
        } else if ("2".equals(type)) {//异动后职务
            intent.putExtra("level", dataList.get(checkedId).get("level").toString());
        }
        intent.putExtra("name", dataList.get(checkedId).get("name").toString());
        intent.putExtra("code", dataList.get(checkedId).get("code").toString());
        intent.putExtra("checkId", checkedId);
        intent.putExtra("userLevel", userLevel);//原有职务等级
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        super.onDestroy();
    }
}
